<?php

function get_data_from_youban($url)
{
	$content = get_data_content($url);
	switch($url)
	{
		case "http://www.youban.com":
			//焦点图
			preg_match_all("/<img src=\"([^\"]*)\" width=\"665\" height=\"255\" alt=\"(?:[^\"]*)\"\/>/",$content, $temp, PREG_PATTERN_ORDER);
			$focus = $temp[1];
			//频道资源
			preg_match_all("/<img width=\"98\" height=\"63\" title=\"([^\"]*)\" alt=\"(?:[^\"]*)\" original=\"([^\"]*)\" src=\"(?:[^\"]*)\" style=\"display: inline;\">/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[1] as $key=>$val)
			{
				$source[] = array(
					'name'=>$val,
					'img'=>$temp[2][$key]
				);
			}
			$topic = array_slice($source, 0, 16);
			$erge = array_slice($source, 16, 32);
			$story = array_slice($source, 48, 32);
			$shizi = array_slice($source, 80, 16);
			$porty = array_slice($source, 96, 16);
			$zhishi = array_slice($source, 112, 16);
			$game = array_slice($source, 128);
			//动画片资源
			preg_match_all("/<img width=\"98\" height=\"100\" title=\"([^\"]*)\" alt=\"(?:[^\"]*)\" original=\"([^\"]*)\" src=\"(?:[^\"]*)\" style=\"display: inline;\">/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[1] as $key=>$val)
			{
				$animation[] = array(
					'name'=>$val,
					'img'=>$temp[2][$key]
				);
			}
			//头部推荐
			preg_match_all("/<a href=\"([^\"]*)\">([^<]*)<s class=\"(?:[^\"]*)\" style=\"height: 15px;\"><\/s><\/a>/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[2] as $key=>$val)
			{
				$toptj[] = array(
					'name'=>$val,
					'link'=>$temp[1][$key]
				);
			}
			/*
			//头部推荐
			preg_match_all("/<a href=\"([^\"]*)\" [^<]*target=\"_blank\"[^<]*>([^<]*)<\/a>/",$content, $temp, PREG_PATTERN_ORDER);
			print_r($temp);exit;
			*/
			$url = "http://www.youban.com/mp3/";
			$content = get_data_content($url);
			preg_match_all("/<a href=\"([^\"]*)\" target=\"_blank\"><img src=\"http:\/\/www.youban.com\/images\/b_g.gif\" original=\"([^\"]*)\" width=\"135\" height=\"88\" alt=\"([^\"]*)\" \/><\/a>/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[3] as $key=>$val)
			{
				$mp3[] = array(
					'name'=>$val,
					'img'=>$temp[2][$key],
					'link'=>$temp[1][$key]
				);
			}

			$result = array(
				'toptj' => $toptj,
				'focus' => $focus,
				'topic' => $topic,
				'channal' => array(
					'erge'=> array(
						'title'=>'儿歌',
						'list'=>$erge
					),
					'story'=> array(
						'title'=>'故事',
						'list'=>$story
					),
					'shizi'=> array(
						'title'=>'识字',
						'list'=>$shizi
					),
					'porty'=> array(
						'title'=>'唐诗',
						'list'=>$porty
					),
					'zhishi'=> array(
						'title'=>'知识',
						'list'=>$zhishi
					),
					'game'=> array(
						'title'=>'游戏',
						'list'=>$game
					),
					'donghua'=> array(
						'title'=>'动画片',
						'list'=>$animation
					),
					'mp3'=> array(
						'title'=>'音频',
						'list'=>$mp3
					)
				)
			);
			return $result;
		break;
		case "http://www.youban.com/erge/":
			//分类推荐
			preg_match_all("/<li><a href=\"([^\"]*)\" target=\"_blank\" .*>([^\<]*)<\/a><\/li>/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[2] as $key=>$val)
			{
				$cates[] = array(
					'name'=>$val,
					'link'=>$temp[1][$key]
				);
			}
			//推荐资源
			preg_match_all("/<a href=\"([^\"]*)\" target=\"_blank\"><img src=\"http:\/\/www.youban.com\/images\/b_g.gif\" original=\"([^\"]*)\" width=\"98\" height=\"63\" alt=\"(?:[^\"]*)\" title=\"([^\"]*)\"\/><\/a>/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[3] as $key=>$val)
			{
				$jptj[] = array(
					'name'=>$val,
					'link'=>$temp[1][$key],
					'img'=>$temp[2][$key]
				);
			}
			//最新
			preg_match_all("/<a href=\"([^\"]*)\" target=\"_blank\">\s+<img src=\"http:\/\/www.youban.com\/images\/b_g.gif\" original=\"([^\"]*)\" width=\"98\" height=\"63\" alt=\"(?:[^\"]*)\" title=\"([^\"]*)\"\/><\/a>/",$content, $temp, PREG_PATTERN_ORDER);

			foreach($temp[3] as $key=>$val)
			{
				if($key < 90)
				{
					$new[] = array(
						'name'=>$val,
						'link'=>$temp[1][$key],
						'img'=>$temp[2][$key]
					);
				}else{
					$hot[] = array(
						'name'=>$val,
						'link'=>$temp[1][$key],
						'img'=>$temp[2][$key]
					);
				}
			}

			$result = array(
				'cates' => $cates,
				'jptj' => $jptj,
				'new' => $new,
				'hot' => $hot
			);
			return $result;
		break;
		case "http://www.youban.com/erge-c15.html":
			preg_match_all("/<p>([^<]*)<\/p>/",$content, $temp, PREG_PATTERN_ORDER);
			$cates = $temp[1];
			preg_match_all("/<div class=\"ListIntrotxt\">([^<]*)<\/div>/",$content, $temp, PREG_PATTERN_ORDER);
			$desc = trim($temp[1][0]);
			preg_match_all("/<img src=\"([^\"]*)\" width=\"176\" height=\"126\" alt=\"([^\"]*)\" title=\"(?:[^\"]*)\"\/>/",$content, $temp, PREG_PATTERN_ORDER);
			$img =  trim($temp[1][0]);
			$name =  trim($temp[2][0]);
			preg_match_all("/<a href=\"([^\"]*)\" target=\"_blank\"><img src=\"([^\"]*)\" width=\"135\" height=\"88\" alt=\"([^\"]*)\" \/><\/a>/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[3] as $key=>$val)
			{
				$jctj[] = array(
					'name' => $val,
					'link' => $temp[1][$key],
					'img' => $temp[2][$key],
				);
			}
			preg_match_all("/<a href=\"([^\"]*)\" target=\"_blank\"><img src=\"http:\/\/www.youban.com\/images\/b_g.gif\" original=\"([^\"]*)\" width=\"135\" height=\"88\" alt=\"([^\"]*)\"\/><\/a>/",$content, $temp, PREG_PATTERN_ORDER);
			foreach($temp[3] as $key=>$val)
			{
				$hot[] = array(
					'name' => $val,
					'link' => $temp[1][$key],
					'img' => $temp[2][$key],
				);
			}
			$new = $hot;
			krsort($hot);

			$result = array(
				'cates' => $cates,
				'cateinfo' => array(
					'name' => $name,
					'desc' => $desc,
					'img' => $img
				),
				'jctj' => $jctj,
				'new' => $new,
				'hot' => $hot
			);
			return $result;
		break;
	}

}

function get_data_content($url)
{
	/** 设置默认编码 */
	header('Content-type: text/html; charset=utf-8');  
    $ch = curl_init();    
    curl_setopt($ch, CURLOPT_URL, $url);    
    curl_setopt($ch, CURLOPT_HEADER, 0);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_REFERER, "http://www.youban.com");
    $content = curl_exec($ch);    
    curl_close($ch);
	return $content;
}

?>